package com.tti.kiosofthercules;


import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsInstanceOf;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.tti.kiosofthercules.TestUtils.sleep;
import static org.hamcrest.Matchers.allOf;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class KioSoftRoomStatusTest {

    @Rule
    public ActivityTestRule<SrcActivity> mActivityTestRule = new ActivityTestRule<>(SrcActivity.class);

    @Test
    public void kioSoftRoomStatusTest() {
//        ApiClient.initClient(Constants.WASHBOARD_REQUEST_BASE_URL);
//        Espresso.registerIdlingResources(ApiClient.getResource());

        ViewInteraction rqrcBtn = onView(
                allOf(withId(R.id.reg_start),isDisplayed()));
        rqrcBtn.perform(click());

        sleep(10000);

        ViewInteraction appCompatEditText = onView(
                allOf(withId(R.id.signin_email), isDisplayed()));
        appCompatEditText.perform(click());

        ViewInteraction appCompatEditText2 = onView(
                allOf(withId(R.id.signin_email), isDisplayed()));
        appCompatEditText2.perform(replaceText("bb@bb.com"), closeSoftKeyboard());

        ViewInteraction appCompatEditText3 = onView(
                allOf(withId(R.id.signin_password), isDisplayed()));
        appCompatEditText3.perform(replaceText("111111"), closeSoftKeyboard());

        ViewInteraction appCompatButton2 = onView(
                allOf(withId(R.id.signin_signin_btn), withText("SIGN IN"), isDisplayed()));
        appCompatButton2.perform(click());

        sleep(3000);

//        ViewInteraction textView = onView(
//                allOf(withId(R.id.balance_acct),
//                        childAtPosition(
//                                allOf(withId(R.id.acnt_bal_container),
//                                        childAtPosition(
//                                                IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
//                                                0)),
//                                1),
//                        isDisplayed()));
//        textView.check(matches(withText("$34.00")));

        ViewInteraction textView2 = onView(
                allOf(withId(R.id.user_id),
                        childAtPosition(
                                childAtPosition(
                                        IsInstanceOf.<View>instanceOf(android.widget.RelativeLayout.class),
                                        1),
                                2),
                        isDisplayed()));
        textView2.check(matches(withText("bb@bb.com")));

        ViewInteraction appCompatImageView = onView(
                allOf(withId(R.id.actionbar_menu_icon), isDisplayed()));
        appCompatImageView.perform(click());

        ViewInteraction appCompatCheckedTextView = onView(
                allOf(withId(R.id.design_menu_item_text), withText("Room Status"), isDisplayed()));
        appCompatCheckedTextView.perform(click());

        sleep(3000);

        ViewInteraction textView3 = onView(
                allOf(withId(R.id.room_name), withText("Room 1"),
                        childAtPosition(
                                allOf(withId(R.id.room_list),
                                        childAtPosition(
                                                IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
                                                0)),
                                0),
                        isDisplayed()));
        textView3.check(matches(withText("Room 1")));

        ViewInteraction textView4 = onView(
                allOf(withId(R.id.room_name), withText("Room 2"),
                        childAtPosition(
                                allOf(withId(R.id.room_list),
                                        childAtPosition(
                                                IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
                                                0)),
                                1),
                        isDisplayed()));
        textView4.check(matches(withText("Room 2")));

        ViewInteraction appCompatTextView = onView(
                allOf(withId(R.id.room_name), withText("Room 1"),
                        childAtPosition(
                                withId(R.id.room_list),
                                0),
                        isDisplayed()));
        appCompatTextView.perform(click());

        sleep(3000);

        ViewInteraction textView5 = onView(
                allOf(withId(R.id.rs_ws_available), withText("6 of 6 Available"),
                        childAtPosition(
                                childAtPosition(
                                        IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
                                        1),
                                1),
                        isDisplayed()));
        textView5.check(matches(withText("6 of 6 Available")));

        ViewInteraction relativeLayout = onView(
                allOf(childAtPosition(
                        allOf(withId(R.id.rs_ws_grid),
                                childAtPosition(
                                        IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
                                        3)),
                        0),
                        isDisplayed()));
        relativeLayout.check(matches(isDisplayed()));

        ViewInteraction relativeLayout2 = onView(
                allOf(childAtPosition(
                        allOf(withId(R.id.rs_ws_grid),
                                childAtPosition(
                                        IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
                                        3)),
                        5),
                        isDisplayed()));
        relativeLayout2.check(matches(isDisplayed()));

        ViewInteraction relativeLayout3 = onView(
                allOf(childAtPosition(
                        allOf(withId(R.id.rs_dr_grid),
                                childAtPosition(
                                        IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
                                        8)),
                        0),
                        isDisplayed()));
        relativeLayout3.check(matches(isDisplayed()));

        ViewInteraction relativeLayout4 = onView(
                allOf(childAtPosition(
                        allOf(withId(R.id.rs_dr_grid),
                                childAtPosition(
                                        IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
                                        8)),
                        5),
                        isDisplayed()));
        relativeLayout4.check(matches(isDisplayed()));

        ViewInteraction textView6 = onView(
                allOf(withId(R.id.rs_dr_status), withText("In Use"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.rs_dr_grid),
                                        0),
                                2),
                        isDisplayed()));
        textView6.check(matches(withText("In Use")));

        ViewInteraction appCompatImageView2 = onView(
                allOf(withId(R.id.actionbar_menu_icon), isDisplayed()));
        appCompatImageView2.perform(click());

        ViewInteraction appCompatImageView3 = onView(
                allOf(withId(R.id.actionbar_menu_icon), isDisplayed()));
        appCompatImageView3.perform(click());

    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}
